package com.sdry.service.impl.zc;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import com.sdry.mapper.zc.AbnormalDisposalMapper;
import com.sdry.mapper.zc.AbnormalDisposalQualityMapper;
import com.sdry.model.zc.AbnormalDisposal;
import com.sdry.model.zc.AbnormalDisposalQuality;
import com.sdry.service.zc.AbnormalDisposalQualityService;

/**
 * @ClassName: AbnormalDisposalQualityServiceImpl 
 * @Description:生产异常处置单品质详情Service实现
 * @Author zc
 * @Date 2018年11月5日 下午12:02:27 
 * @version 1.0
 */
@Service
public class AbnormalDisposalQualityServiceImpl implements AbnormalDisposalQualityService {

	@Resource
	private AbnormalDisposalQualityMapper abnormalDisposalQualityMapper;
	@Resource
	private AbnormalDisposalMapper abnormalDisposalMapper;	
	/**
	 * 查询生产异常处置单品质详情
	 * @param abnormalDisposalQuality
	 * @return
	 */
	public AbnormalDisposalQuality findAbnormalDisposalQualityByAdID(AbnormalDisposalQuality abnormalDisposalQuality) {
		// TODO Auto-generated method stub
		return abnormalDisposalQualityMapper.findAbnormalDisposalQualityByAdID(abnormalDisposalQuality);
	}

	/**
	 * 新增生产异常处置单品质详情
	 * @param abnormalDisposalQuality
	 * @return
	 */
	@Transactional(rollbackFor={Exception.class})
	public Integer insertAbnormalDisposalQuality(AbnormalDisposalQuality abnormalDisposalQuality,String isQualified) {
		// TODO Auto-generated method stub
		int isSuccess = 0;
		try {
			abnormalDisposalQuality.setReserve1("0");
			isSuccess = abnormalDisposalQualityMapper.insertAbnormalDisposalQuality(abnormalDisposalQuality);
			AbnormalDisposal abnormalDisposal = abnormalDisposalMapper.findAbnormalDisposalByID(abnormalDisposalQuality.getAdID());
			if ("0".equals(isQualified)) {
				if ("0".equals(abnormalDisposalQuality.getReserve3())) {
					abnormalDisposal.setReserve1("2");
				} else {
					abnormalDisposal.setReserve1("-1");
				}
			} else {
				abnormalDisposal.setReserve1("5");
			}
			isSuccess = abnormalDisposalMapper.setStatus(abnormalDisposal);
		} catch (Exception e) {
			e.printStackTrace();     
	        TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
	        isSuccess = 0;
		}
		return isSuccess;
	}

	/**
	 * 修改生产异常处置单品质详情
	 * @param abnormalDisposalQuality
	 * @return
	 */
	@Transactional(rollbackFor={Exception.class})
	public Integer updateAbnormalDisposalQuality(AbnormalDisposalQuality abnormalDisposalQuality) {
		// TODO Auto-generated method stub
		int isSuccess = 0;
		try {
			AbnormalDisposalQuality abnormalDisposalQuality2 = abnormalDisposalQualityMapper.findAbnormalDisposalQualityByAdID(abnormalDisposalQuality);
			if (abnormalDisposalQuality.getReserve1().equals("0")) {
				abnormalDisposalQuality2.setReserve1("1");
				abnormalDisposalQuality2.setTechniquePerson(abnormalDisposalQuality.getTechniquePerson());
				abnormalDisposalQuality2.setOpinion2(abnormalDisposalQuality.getOpinion2());
				abnormalDisposalQuality2.setReserve4(abnormalDisposalQuality.getReserve4());
			} else {
				abnormalDisposalQuality2.setReserve1("2");
				abnormalDisposalQuality2.setMakePerson(abnormalDisposalQuality.getMakePerson());
				abnormalDisposalQuality2.setOpinion3(abnormalDisposalQuality.getOpinion3());
				abnormalDisposalQuality2.setReserve5(abnormalDisposalQuality.getReserve5());
			}
			isSuccess = abnormalDisposalQualityMapper.updateAbnormalDisposalQuality(abnormalDisposalQuality2);
			AbnormalDisposal abnormalDisposal = abnormalDisposalMapper.findAbnormalDisposalByID(abnormalDisposalQuality.getAdID());
			if ("0".equals(abnormalDisposalQuality2.getReserve4())) {
				if (abnormalDisposalQuality.getReserve1().equals("0")) {
					abnormalDisposal.setReserve1("3");
				} else {
					if("0".equals(abnormalDisposalQuality2.getReserve5())){
						abnormalDisposal.setReserve1("5");
					}else{
						abnormalDisposal.setReserve1("-1");
					}
				}
			} else {
				abnormalDisposal.setReserve1("-1");
			}
			isSuccess = abnormalDisposalMapper.setStatus(abnormalDisposal);
		} catch (Exception e) {
			e.printStackTrace();     
	        TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
	        isSuccess = 0;
		}
		return isSuccess;
	}

}
